//package cn.nfwx.tv.activity;
//
//import android.app.Activity;
//import android.app.Dialog;
//import android.content.Context;
//import android.content.Intent;
//import android.os.Bundle;
//import android.support.v7.widget.RecyclerView;
//import android.view.Display;
//import android.view.KeyEvent;
//import android.view.View;
//import android.view.Window;
//import android.view.WindowManager;
//import android.view.inputmethod.EditorInfo;
//import android.view.inputmethod.InputMethodManager;
//import android.widget.EditText;
//import android.widget.FrameLayout;
//import android.widget.ImageView;
//import android.widget.LinearLayout;
//import android.widget.ListView;
//import android.widget.TextView;
//
//import com.chad.library.adapter.base.BaseQuickAdapter;
//import com.google.android.flexbox.AlignItems;
//import com.google.android.flexbox.FlexDirection;
//import com.google.android.flexbox.FlexWrap;
//import com.google.android.flexbox.FlexboxLayoutManager;
//import com.google.gson.Gson;
//import com.google.gson.reflect.TypeToken;
//import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
//import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;
//
//import org.greenrobot.eventbus.EventBus;
//import org.greenrobot.eventbus.Subscribe;
//import org.greenrobot.eventbus.ThreadMode;
//
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//
//import cn.nfwx.tv.R;
//import cn.nfwx.tv.adapter.SearchHistoryAdapter;
//import cn.nfwx.tv.adapter.SearchHotAdapter;
//import cn.nfwx.tv.base.BaseActivity;
//import cn.nfwx.tv.base.NFWXApplication;
//import cn.nfwx.tv.controller.SearchResultSubController;
//import cn.nfwx.tv.controller.SearchResultViewController;
//import cn.nfwx.tv.dao.AppiontmentDao;
//import cn.nfwx.tv.events.AppiontmentEvent;
//import cn.nfwx.tv.events.PlayEvents;
//import cn.nfwx.tv.events.PlayLiveEvents;
//import cn.nfwx.tv.events.QpopPlayEvent;
//import cn.nfwx.tv.helper.ThreadHelper;
//import cn.nfwx.tv.manager.SearchManager;
//import cn.nfwx.tv.model.AppiontModel;
//import cn.nfwx.tv.model.AppiontmentModel;
//import cn.nfwx.tv.model.ColumnModel;
//import cn.nfwx.tv.model.KeywordModel;
//import cn.nfwx.tv.model.LiveModel;
//import cn.nfwx.tv.model.SubscriptionModel;
//import cn.nfwx.tv.model.TilteModel;
//import cn.nfwx.tv.util.AppUtil;
//import cn.nfwx.tv.util.SharePrefUtil;
//import cn.nfwx.tv.util.TSnackbarUtil;
//import cn.nfwx.tv.widget.LoadView;
//import okhttp3.Call;
//import okhttp3.Callback;
//import okhttp3.Response;
//
//public class SearchActivity extends BaseActivity implements View.OnClickListener,BaseQuickAdapter.OnItemClickListener {
//
//    private final String TAG = SearchActivity.class.getSimpleName();
//
//    private ImageView search_back_btn, search_edit_btn;
//    private ListView search_hot_listView;
//    private RecyclerView search_history_recyclerView;
//    private LinearLayout search_main_rel;
//    private EditText et_keyword;
//    private FrameLayout fl_content;
//
//    private SearchResultViewController searchResultViewController;
//    private SearchResultSubController searchResultSubController;
//    private SearchHistoryAdapter searchHistoryAdapter;
//    private InputMethodManager imm;
//    private SearchHotAdapter hotAdapter;
//    private LoadView loadView;
//
//    private String requestHotKeywordCommand;
//    private int count = 15;
//
//
//    public static void startActivity(Activity activity) {
//        Intent intent = new Intent(activity, SearchActivity.class);
//        activity.startActivity(intent);
//    }
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_search);
//        AppUtil.getInstance().addActivity(SearchActivity.this);
//        EventBus.getDefault().register(this);
//        initView();
//    }
//
//    @Override
//    protected String activityName() {
//        return SearchActivity.class.getSimpleName();
//    }
//
//    @Override
//    protected void onStop() {
//        super.onStop();
//    }
//
//    public void initView() {
//        searchResultViewController = new SearchResultViewController(this);
//        searchResultSubController = new SearchResultSubController(this);
//        search_main_rel = (LinearLayout) this.findViewById(R.id.search_main_rel);
//        et_keyword = (EditText) findViewById(R.id.et_keyword);
//        search_history_recyclerView = (RecyclerView) this.findViewById(R.id.search_history_recyclerView);
//        fl_content = (FrameLayout) findViewById(R.id.fl_content);
//        search_hot_listView = (ListView) this.findViewById(R.id.search_hot_listView);
//        search_back_btn = (ImageView) this.findViewById(R.id.search_back_btn);
//        search_edit_btn = (ImageView) this.findViewById(R.id.search_edit_btn);
//        findViewById(R.id.iv_clear_history).setOnClickListener(this);
//        findViewById(R.id.ll_content).setOnClickListener(this);
//        findViewById(R.id.ll_content).requestFocus();
//        hotAdapter = new SearchHotAdapter(this);
//        search_hot_listView.setAdapter(hotAdapter);
//        search_back_btn.setOnClickListener(this);
//        search_edit_btn.setOnClickListener(this);
//        loadView = new LoadView(getBaseContext());
//        loadView.setOnLoadClickLisener(new LoadView.OnLoadClickListener() {
//            @Override
//            public void onreload() {
//                initData();
//            }
//        });
//
//
//        FlexboxLayoutManager manager = new FlexboxLayoutManager();
//        //设置主轴排列方式
//        manager.setFlexDirection(FlexDirection.ROW);
//        //设置是否换行
//        manager.setFlexWrap(FlexWrap.WRAP);
//        manager.setAlignItems(AlignItems.BASELINE);
//        search_history_recyclerView.setLayoutManager(manager);
//        search_history_recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).size(30).build());
//        search_history_recyclerView.addItemDecoration(new VerticalDividerItemDecoration.Builder(this).size(10).build());
//        searchHistoryAdapter = new SearchHistoryAdapter(this);
//        search_history_recyclerView.setAdapter(searchHistoryAdapter);
//
//        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//        initData();
//        initListener();
//    }
//
//    private void initData() {
//        ArrayList<String> keywords = SharePrefUtil.getSearchKeyWord(this);
//        if (keywords != null && !keywords.isEmpty()) {
//            searchHistoryAdapter.setNewData(keywords);
//        }
//
//        List<LiveModel> liveModels = NFWXApplication.getInstance().getHotLives();
//        for (LiveModel liveModel : liveModels) {
//            liveModel.loadData();
//        }
//
//        requestHotKeywordCommand = SearchManager.requestHotKeywordResult(this, count, new Callback() {
//            @Override
//            public void onFailure(final Call call, IOException e) {
//                ThreadHelper.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (call.request().tag().equals(requestHotKeywordCommand)) {
//                            if (loadView.getParent() == null) {
//                                fl_content.addView(loadView);
//                            }
//                            loadView.loadErrorSearch();
//                        }
//                    }
//                });
//            }
//
//            @Override
//            public void onResponse(final Call call, final Response response) throws IOException {
//                ThreadHelper.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            if (call.request().tag().equals(requestHotKeywordCommand)) {
//                                if (response.code() == 200) {
//                                    String message = response.body().string();
//                                    Gson gson = new Gson();
//                                    List<KeywordModel> keywordModels = gson.fromJson(message, new TypeToken<List<KeywordModel>>() {
//                                    }.getType());
//                                    hotAdapter.setNewData(keywordModels);
//                                    if (loadView.getParent() != null) {
//                                        fl_content.removeView(loadView);
//                                    }
//                                }
//                            }
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });
//            }
//        });
//    }
//
//    private void initListener() {
//        searchHistoryAdapter.setOnItemClickListener(new SearchHistoryAdapter.OnItemClickListener() {
//            @Override
//            public void itemClick(RecyclerView.Adapter adapter, View view, int position, String keyword) {
//                search_main_rel.setVisibility(View.GONE);
//                et_keyword.setText(keyword);
//                searchResultViewController.setData(keyword);
//                searchResultViewController.show();
//                SharePrefUtil.saveSearchKeyWord(SearchActivity.this, keyword);
//                initData();
//            }
//        });
//
//        hotAdapter.setOnItemClickListener(new SearchHotAdapter.OnItemClickListener() {
//            @Override
//            public void itemClick(View view, int position, String keyword) {
//                search_main_rel.setVisibility(View.GONE);
//                et_keyword.setText(keyword);
//                searchResultViewController.setData(keyword);
//                searchResultViewController.show();
//                SharePrefUtil.saveSearchKeyWord(SearchActivity.this, keyword);
//                initData();
//            }
//        });
//
//        et_keyword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId,
//                                          KeyEvent event) {
//                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//                    search_main_rel.setVisibility(View.GONE);
//                    String keyword = et_keyword.getEditableText().toString();
//                    et_keyword.setText(keyword);
//                    searchResultViewController.setData(keyword);
//                    searchResultViewController.show();
//                    SharePrefUtil.saveSearchKeyWord(SearchActivity.this, keyword);
//                    initData();
//                }
//                return false;
//            }
//        });
//    }
//
//
//    @Override
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.search_back_btn:
//                if (searchResultSubController.isShow()) {
//                    searchResultSubController.hidden();
//                    searchResultViewController.show();
//                } else if (search_main_rel.getVisibility() == View.GONE) {
//                    search_main_rel.setVisibility(View.VISIBLE);
//                    searchResultViewController.hidden();
//                    et_keyword.setText("");
//                } else if (search_main_rel.getVisibility() == View.VISIBLE) {
//                    finish();
//                }
//                break;
//            case R.id.search_edit_btn:
//                search_main_rel.setVisibility(View.GONE);
//                String keyWord = et_keyword.getEditableText().toString();
//                searchResultViewController.setData(keyWord);
//                SharePrefUtil.saveSearchKeyWord(this, keyWord);
//                initData();
//                searchResultViewController.show();
//                if (imm != null) {
//                    imm.hideSoftInputFromWindow(et_keyword.getWindowToken(), 0);
//                }
//                break;
//            case R.id.iv_clear_history:
//                Dialog dialog = getDialog(R.layout.dialog_sreach_delete);
//                dialog.show();
//                break;
//            case R.id.ll_content:
//                if (imm != null) {
//                    imm.hideSoftInputFromWindow(et_keyword.getWindowToken(), 0);
//                }
//                break;
//            case R.id.et_keyword:
//                et_keyword.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI | EditorInfo.IME_FLAG_NO_FULLSCREEN);
//                break;
//        }
//    }
//
//    public void refresh() {
//        searchResultViewController.refresh();
//    }
//
//    public void loadmore() {
//        searchResultViewController.loadmore();
//    }
//
//    /**
//     * 显示栏目详情
//     * @param subscriptionModel
//     */
//    public void showSubscriptionDetail(SubscriptionModel subscriptionModel) {
//        search_main_rel.setVisibility(View.GONE);
//        searchResultViewController.hidden();
//        searchResultSubController.show(subscriptionModel);
//    }
//
//    public Dialog getDialog(int resId) {
//        final Dialog dialog = new Dialog(SearchActivity.this, R.style.MyDialog);
//        View dialogView = View.inflate(SearchActivity.this, resId, null);
//        TextView cancle_btn = dialogView.findViewById(R.id.btn_logout_cancle);
//        TextView sure_btn = dialogView.findViewById(R.id.btn_logout_sure);
//        sure_btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                dialog.cancel();
//                SharePrefUtil.clearSearchKeyWord(SearchActivity.this);
//                searchHistoryAdapter.clearData();
//            }
//        });
//        cancle_btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                dialog.cancel();
//            }
//        });
//        dialog.setContentView(dialogView);
//        Window dialogWindow = dialog.getWindow();
//        WindowManager m = SearchActivity.this.getWindowManager();
//        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高度
//        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
//        p.height = (int) (d.getHeight() * 0.8); // 高度设置为屏幕的0.6，根据实际情况调整
//        p.width = (int) (d.getWidth() * 0.6); // 宽度设置为屏幕的0.65，根据实际情况调整
//        dialogWindow.setAttributes(p);
//        return dialog;
//    }
//
//    /**
//     * @param event
//     */
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void handlerAppiontEvent(final AppiontmentEvent event) {
//        if (event == null || event.getAppiontModel() == null) {
//            return;
//        }
//        final AppiontModel appiontModel = event.getAppiontModel();
//        AppiontmentDao dao = new AppiontmentDao(getBaseContext());
//        AppiontmentModel appiontmentModel = dao.queryForId(appiontModel.getId());
//        appiontmentModel.setHasLook(true);
//        dao.update(appiontmentModel);
//
//        TSnackbarUtil.showTSnackbar(findViewById(R.id.ll_content), getString(R.string.appiontment_note, event.getAppiontModel().getName()), new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                EventBus.getDefault().post(new PlayLiveEvents(appiontModel.getLid(), appiontModel.getLiveName()));
//                finish();
//            }
//        });
//    }
//
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void qpopPlayerEvent(QpopPlayEvent qpopPlayEvent) {
//       finish();
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        AppUtil.getInstance().finishActivity(SearchActivity.this);
//        EventBus.getDefault().unregister(this);
//    }
//
//    @Override
//    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//        if (position < adapter.getData().size()) {
//            if (adapter.getData().get(position) instanceof TilteModel) { // 加载，收起
//                // 获取多余的部分
//                List<Object> objects = searchResultViewController.getMoreList();
//                TilteModel tilteModel = (TilteModel) adapter.getData().get(position);
//                if (tilteModel.isLoadMore) {
//                    for (int j = 0; j < objects.size(); j++) {
//                        adapter.remove(position - 1 - j);
//                    }
//                    ((TextView) view).setText(R.string.load_more);
//                    tilteModel.isLoadMore = false;
//                } else {
//                    adapter.addData(position, objects);
//                    ((TextView) view).setText(R.string.ellipsis);
//                    tilteModel.isLoadMore = true;
//                }
//            } else if (adapter.getData().get(position) instanceof ColumnModel) { // 播放对应的视频
////                ColumnModel columnModel = (ColumnModel) adapter.getData().get(position);
////                VodPlayerActivity.startActivity(this, columnModel.getPlaypath(), null, columnModel.getName(), 0);
////                EventBus.getDefault().post(new PlayEvents(columnModel.getPlaypath(), null, columnModel.getName(), 0));
//                //getActivity().finish();
//            } else if (adapter.getData().get(position) instanceof SubscriptionModel) {
//                SubscriptionModel subscriptionModel = (SubscriptionModel) adapter.getData().get(position);
//                showSubscriptionDetail(subscriptionModel);
//            }
//        }
//    }
//}
